package com.saicmotor.springboot_demo.controller;

import com.saicmotor.springboot_demo.model.Role;
import com.saicmotor.springboot_demo.model.json_pojo.JsonResult;
import com.saicmotor.springboot_demo.model.json_pojo.RoleUpdate;
import com.saicmotor.springboot_demo.service.CommonService;
import com.saicmotor.springboot_demo.service.RoleService;
import com.saicmotor.springboot_demo.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    CommonService commonService;
    @Autowired
    UserRoleService userRoleService;
    @Autowired
    JsonResult jsonResult;

    @GetMapping("/all")
    public JsonResult getAll() {
        return jsonResult.data(roleService.getAll());
    }

    @PostMapping("/add")
    public JsonResult insertInfoOfRole(@RequestBody @Validated Role role) {   //http://localhost:8080/user/info?name=setName&age=setAge
        if (commonService.checkName("role", role.getName())) {
            return jsonResult.msg(role.getName() + " has Existed");
        } else {
            roleService.insertRoleInfo(role.getName());
            return jsonResult.msg("Insert [" + role.getName() + "] Complete");
        }
    }

    @DeleteMapping(value = "/delete")
    public JsonResult deleteUser(@RequestBody @Validated Role role) {
        if(!commonService.checkName("role",role.getName())){
            return jsonResult.msg("[" + role.getName() + "] Is Not Exist");
        }else {
            roleService.deleteByName(role.getName());
            userRoleService.deleteByRoleName(role.getName());
            return jsonResult.msg("Delete [" + role.getName() + "] Complete");
        }
    }

    @PutMapping("/update")
    public JsonResult updateAgeOfUser(@RequestBody @Validated RoleUpdate roleUpdate) {
        if (!commonService.checkName("role", roleUpdate.getOldName())) {
            return jsonResult.msg(roleUpdate.getOldName() + " Is Not Exist");
        } else if (commonService.checkName("role", roleUpdate.getNewName())) {
            return jsonResult.msg(roleUpdate.getNewName() + " Has Existed");
        } else {
            roleService.updateName(roleUpdate.getNewName(), roleUpdate.getOldName());
            return jsonResult.msg("Update [" + roleUpdate.getOldName() + "] Complete");
        }
    }
    @PostMapping("/create")
    public JsonResult RoleTableCreation() {
        if (commonService.checkTable("role")) {
            return jsonResult.msg("[role] Table Exists");
        } else {
            roleService.createRoleTable("role");
            return jsonResult.msg("Create [role] Table Complete");
        }
    }

    @PostMapping("/create/init")
    public JsonResult initRoleTable() {
        commonService.cleanTable("role");
        roleService.initRoleTable();

        return jsonResult.msg("Init [role] Table Complete");
    }
}
